package com.android.djlmapp.activity.write;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.listener.read.GetPhotoUrlListener;
import com.android.djlmapp.tools.read.WriteCardUtils;
import com.android.readcard.R;
import com.android.readcard.activity.WriteCardActivity;
import com.android.readcard.bean.ReadIccidAndWriteCardInfo;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventBusUtil;
import com.android.readcard.eventbus.EventCodes;

/**
 * 调用写卡案例
 */
public class TestWriteActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_write);
        initView();
        initData();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        // 跳转写卡界面, 0代表废卡重置,1代表写卡,2代表补卡
        startActivity(new Intent(TestWriteActivity.this, WriteCardActivity.class).putExtra("WriteType", 2));

    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    private String orderSn = "201901055510256509"; // 订单号

    @Override
    protected void receiveEvent(Event event) {

        String mUrl = "";
        String sucUrl = "";

        // 接受到Event后的相关逻辑
        switch (event.getCode()) {
            case EventCodes.EventReadCardCode.B: // 返回iccid
                ReadIccidAndWriteCardInfo data = (ReadIccidAndWriteCardInfo) event.getData();
                Log.e("项目异常写卡", event.getCode() + "------ReadIccidAndWriteCardInfo--返回iccid----" + data.toString());
                String iccidNum = data.getIccidNum();
                int writeType = data.getWriteType();
                String errMsg = data.getErrMsg(); // 错误信息
                String imsi = data.getImsi(); // imsi
                switch (writeType) {
                    case 0: // 废卡重置
                        Log.e("项目异常写卡", "------ReadIccidAndWriteCardInfo--废卡重置----" + data.toString());
                        WriteCardUtils.clearCard(iccidNum, imsi, new GetPhotoUrlListener() {
                            @Override
                            public void backPhotoUrl(int backType, String result) {
                                // eventbus通知WriteCardActivity,废卡重置完成了,成功失败都要提示并且关闭写卡界面
                                Event<String> clearCardEvent = new Event<>(EventCodes.EventReadCardCode.E);
                                EventBusUtil.sendEvent(clearCardEvent);
                                WriteCardUtils.showMsg(TestWriteActivity.this, "废卡重置结果", result);
                            }
                        });
                        break;
                    case 1: // 写卡
                        Log.e("项目异常写卡", "------ReadIccidAndWriteCardInfo--写卡----" + data.toString());
                        mUrl = GlobalUrls.BAI_KA;
                        // 上传iccid获取短信中心号和imsi并eventbus返回给WriteCardActivity
                        WriteCardUtils.startWrite(TestWriteActivity.this, mUrl, iccidNum, orderSn, "写卡");
                        break;
                    case 2: // 补卡
                        Log.e("项目异常写卡", "------ReadIccidAndWriteCardInfo--补卡----" + data.toString());
                        mUrl = GlobalUrls.BU_KA;
                        // 上传iccid获取短信中心号和imsi并eventbus返回给WriteCardActivity
                        WriteCardUtils.startWrite(TestWriteActivity.this, mUrl, iccidNum, orderSn, "补卡");
                        break;
                }

                break;
            case EventCodes.EventReadCardCode.D: // 返回写卡结果
                ReadIccidAndWriteCardInfo xiekaData = (ReadIccidAndWriteCardInfo) event.getData();
                Log.e("项目异常写卡", event.getCode() + "------ReadIccidAndWriteCardInfo-返回写卡结果-----" + xiekaData.toString());
                String iccidNum1 = xiekaData.getIccidNum();
                int writeType1 = xiekaData.getWriteType();
                String errMsg1 = xiekaData.getErrMsg(); // 错误信息
                String imsi1 = xiekaData.getImsi(); // imsi
                // 判断errMsg是不是空,是空值就是写卡成功
                if (TextUtils.isEmpty(errMsg1)) {

                    switch (writeType1) {
                        case 1:
                            sucUrl = GlobalUrls.BAI_KA_OK;
                            WriteCardUtils.writeSucSubmit(TestWriteActivity.this, sucUrl, iccidNum1, orderSn, "写卡", null);
                            break;
                        case 2:
                            sucUrl = GlobalUrls.BU_KA_OK;
                            WriteCardUtils.writeSucSubmit(TestWriteActivity.this, sucUrl, iccidNum1, orderSn, "补卡", null);
                            break;
                    }
                } else {
                    // 写卡失败,提示错误
                    Event<String> clearCardEvent = new Event<>(EventCodes.EventReadCardCode.E);
                    EventBusUtil.sendEvent(clearCardEvent);
                    switch (writeType1) {
                        case 1:
                            WriteCardUtils.showMsg(TestWriteActivity.this, "写卡结果", errMsg1);
                            break;
                        case 2:
                            WriteCardUtils.showMsg(TestWriteActivity.this, "补卡结果", errMsg1);
                            break;
                    }
                }
                break;
        }
    }
}
